package edu.uwm.cs552;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;

import javax.swing.SwingConstants;

/**
 * A component that displays a hexboard
 */
public class HexBoardPanel extends TopographyBoardPanel implements Observer {
	/**
	 * Keep Eclipse happy
	 */
	private static final long serialVersionUID = 1L;
	
	private HexBoard hexBoard;
	
	/**
	 * Create a hex panel that views this board.
	 * Initially the scale is 20.
	 * By default, the mesh is off and no coordinate is selected.
	 * @param b initial board, must not be null
	 */
	public HexBoardPanel() {
	}
	

	/**
	 * Set the size of this panel if the background or magnification changes.
	 */
	public void resize() {
		this.setSize((int)hexBoard.getWidth(), (int)hexBoard.getHeight());
		this.setPreferredSize(this.getSize());
		repaint();
	}
	
	
	@Override 
	public void paint(Graphics g) {
		super.paint(g);
		hexBoard.draw(g);
	}

	/// The following methods are used to making scroll bars work.
	
	static final double SQRT32 = Math.sqrt(3)/2;

	public int getScrollableUnitIncrement(Rectangle arg0, int orientation, int direction) {
		// if some clicks the control, we go one hexagon width/height at a time.
		if (orientation == SwingConstants.HORIZONTAL) {
			return (int)hexBoard.getScale();
		} else {
			return (int)(hexBoard.getScale()*SQRT32);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void update(Observable o, Object arg) {
		if (!(o instanceof HexBoard)) return;
		hexBoard = (HexBoard) o;
		if (arg != null) {
			if (arg instanceof HasTopography)
				((HasTopography)arg).draw(this.getGraphics(), hexBoard.getScale(), hexBoard.getShowHidden());
		}
		else {
			resize();
		}
		
	}
	
	
}
